package com.winbomb.kingcard.base;

import android.util.Log;

import com.winbomb.kingcard.GameMsg;
import com.winbomb.kingcard.IBattle;
import com.winbomb.kingcard.ICard;
import com.winbomb.kingcard.IGameLogic;
import com.winbomb.kingcard.IPlayer;
import com.winbomb.kingcard.IState;
import com.winbomb.kingcard.NoMoreCardException;
import com.winbomb.kingcard.Pos;
import com.winbomb.kingcard.INegotiate.NegoResult;
import com.winbomb.kingcard.base.anim.IPlayable;
import com.winbomb.kingcard.base.anim.impl.ActionAnim;
import com.winbomb.kingcard.base.anim.impl.MovingCardsAnim;
import com.winbomb.kingcard.screens.BattleScreen;
import com.winbomb.kingcard.utils.GameUtils;

public enum GameState implements IState {

	GAME_START {
		public boolean isWaitInput() {
			return false;
		}

		public void nextState(IGameLogic game) {
			if (game.getCurrTurn().isAlive()) {
				game.getCurrTurn().drawCard();
			}
			game.nextTurn();

			// 最后一个Player的手牌张数
			int cardCnt = game.getPlayers()[game.getPlayerCount() - 1].getCardCount();
			if (cardCnt >= GameLogic.INIT_CARD_COUNT) {
				GameMsg msg = new GameMsg(0);
				msg.add("游戏开始.");
				game.getGameRecorder().add(msg);

				msg = new GameMsg(0);
				msg.add("进入第 1 回合.");
				game.getGameRecorder().add(msg);

				msg = new GameMsg(0);
				msg.addPlayer(game.getCurrBanker().getPlayerName());
				msg.add(" 回合开始.");
				game.getGameRecorder().add(msg);

				game.setState(SELECT_ACTION);
			}
		}
	},
	NEW_ROUND {
		public boolean isWaitInput() {
			return false;
		}

		public void nextState(IGameLogic game) {
			IPlayer currPlayer = game.getCurrPlayer();
			if (currPlayer.isAlive()) {
				addAnimation(game, AnimManager.ANIM_DEALING_CARD);
			}
		}
	},
	/**
	 * 选择本回合的动作
	 */
	SELECT_ACTION {
		public void nextState(IGameLogic game) {
			IPlayer currTurn = game.getCurrTurn();
			ActionAnim actionAnim = AnimManager.ACTION_ANIM;
			actionAnim.reset();
			actionAnim.setTargetPlayer(currTurn);
			actionAnim.setAction(currTurn.getAction());
			addAnimation(game, actionAnim);

			// 如果当前玩家是human,让游戏取消暂停,不然会出现停顿感
			if (currTurn.isHuman()) {
				game.setPause(false);
			}

			switch (currTurn.getAction()) {
			case BATTLE:
				game.setState(SELECT_TARGET);
				break;

			case REST:
				// 添加发牌的动画
				MovingCardsAnim anim = AnimManager.MOVING_CARDS_ANIM;
				anim.reset();

				anim.setStartPos(new Pos(220, 100));
				anim.setTargetPos(currTurn.getPlayerInfo().getFramePos());
				anim.setTargetPlayer(currTurn);

				ICard[] cards = new ICard[1];
				try {
					ICard topCard = game.getCardPile().drawCard();
					cards[0] = topCard;
				} catch (NoMoreCardException ex) {
					Log.w(TAG, ex);
				}
				anim.setCards(cards);
				anim.setOpened(currTurn.isHuman());
				addAnimation(game, anim);

				game.setState(SELECT_CHANGE_CARD);
				break;

			case TAKE_POWER:
				game.setState(TAKE_POWER);
				break;

			case SEND_OUT:
				game.setState(SELECT_SENDOUT_TARGET);
				break;

			case CALL_UP:
				game.setState(SELECT_CALLUP_SOURCE);
				break;
			}

		}
	},
	SELECT_CHANGE_CARD {
		public void nextState(IGameLogic game) {
			game.getCurrTurn().changeCard();
			game.setState(ROUND_END);
		}
	},
	SELECT_TARGET {
		public void nextState(IGameLogic game) {
			game.setState(SELECT_ATK_CARDS);
		}
	},
	SELECT_ATK_CARDS {
		public void nextState(IGameLogic game) {
			IPlayer attacker = game.getCurrTurn();
			IPlayer defender = attacker.getAttackTarget();
			attacker.playCards();
			game.getBattle().newBattle(attacker, defender);
			addAnimation(game, AnimManager.ANIM_MARCH);
		}
	},
	SELECT_DEF_CARDS {
		public void nextState(IGameLogic game) {
			IBattle battle = game.getBattle();
			battle.start();
			if (!battle.getInvader().isHuman() && !battle.getGuarder().isHuman()) {
				addAnimation(game, AnimManager.ANIM_BATTLE);
			} else {
				GameLogic gameImpl = (GameLogic) game;
				gameImpl.getGame().setScreen(new BattleScreen(gameImpl.getGame(), gameImpl));
			}
			game.setState(BattleState.BATTLE_START);
		}
	},

	/**
	 * 开始交涉
	 */
	BEGIN_NEGOTIATE {
		public void nextState(IGameLogic game) {
			NegoResult result = game.getNegotiate().getResult();
			IPlayer currTurn = game.getCurrTurn();
			IPlayer currPlayer = game.getCurrPlayer();
			switch (result) {

			case REQUIRE:
				// 随机抽一张牌
				if (currTurn.getCardCount() > 0) {
					ICard card = GameUtils.selectRandomCard(currTurn.getCards());
					card.setSelected(true);
					ICard[] cards = currTurn.takeSelectedCards();

					// 添加Moving动画
					MovingCardsAnim anim = AnimManager.MOVING_CARDS_ANIM;
					anim.reset();
					anim.setStartPos(currTurn.getPlayerInfo().getFramePos());
					anim.setTargetPos(currPlayer.getPlayerInfo().getFramePos());
					anim.setTargetPlayer(currPlayer);
					anim.setCards(cards);
					anim.setOpened(currTurn.isHuman() || currPlayer.isHuman());

					addAnimation(game, anim);
				}

			case AGREE:
				game.getNegotiate().finish();
				game.setCurrPlayer(currTurn);
				currTurn.getAttackRoute().move();
				addAnimation(game, AnimManager.ANIM_MARCH);
				break;

			case DENY:
				// 更换进攻目标
				currTurn.setAttackTarget(currPlayer);
				game.getBattle().newBattle(currTurn, currPlayer);
				game.setCurrPlayer(currPlayer);
				game.setState(GameState.SELECT_DEF_CARDS);
				break;

			}
		}
	},

	/**
	 * 选择要派遣的对象
	 * 
	 * 派遣的对象可以是任何自己旗下的势力(包括灭亡的势力, 灭亡的势力在收到
	 * 
	 * 三张派遣的牌时,可以额外从牌堆中抽两张牌进行复活)
	 */
	SELECT_SENDOUT_TARGET {
		public void nextState(IGameLogic game) {
			game.setState(SELECT_SENDOUT_CARDS);
		}
	},

	/**
	 * 选择派遣的牌(最多三张)
	 */
	SELECT_SENDOUT_CARDS {
		public void nextState(IGameLogic game) {

			IPlayer currTurn = game.getCurrTurn();
			ICard[] sendCards = currTurn.takeSelectedCards();
			IPlayer sendTarget = currTurn.getSendTarget();

			MovingCardsAnim movingAnim = AnimManager.MOVING_CARDS_ANIM;
			movingAnim.reset();
			movingAnim.setStartPos(currTurn.getPlayerInfo().getFramePos());
			movingAnim.setTargetPos(sendTarget.getPlayerInfo().getFramePos());
			movingAnim.setSourcePlayer(currTurn);
			movingAnim.setTargetPlayer(sendTarget);
			movingAnim.setCards(sendCards);
			movingAnim.setOpened(currTurn.isHuman() || sendTarget.isHuman());

			addAnimation(game, movingAnim);
			game.setState(ROUND_END);
		}
	},

	/**
	 * 选择征召的对象
	 * 
	 * 征召的对象可以是任何自己旗下存活的势力
	 * 
	 */
	SELECT_CALLUP_SOURCE {
		public void nextState(IGameLogic game) {
			game.setState(SELECT_CALLUP_CARDS);
		}
	},

	/**
	 * 选择征召的牌(最多两张)
	 */
	SELECT_CALLUP_CARDS {

		/**
		 * 这里先不让玩家参与选牌,直接随机抽两张牌
		 */
		public boolean isWaitInput() {
			return false;
		}

		public void nextState(IGameLogic game) {

			IPlayer currTurn = game.getCurrTurn();
			IPlayer srcPlayer = currTurn.getCallSource();

			ICard[] srcCards = srcPlayer.getCards();
			int cardCnt = (srcCards.length >= 3) ? 2 : 1;
			ICard[] calledCards = GameUtils.selectRandomCard(srcCards, cardCnt);
			for (ICard card : calledCards) {
				card.setSelected(true);
			}
			ICard[] destCards = srcPlayer.takeSelectedCards();

			MovingCardsAnim movingAnim = AnimManager.MOVING_CARDS_ANIM;
			movingAnim.reset();

			movingAnim.setStartPos(srcPlayer.getPlayerInfo().getFramePos());
			movingAnim.setTargetPos(currTurn.getPlayerInfo().getFramePos());
			movingAnim.setCards(destCards);
			movingAnim.setTargetPlayer(currTurn);

			boolean isOpened = (currTurn.isHuman() || srcPlayer.isHuman());
			movingAnim.setOpened(isOpened);

			addAnimation(game, movingAnim);

			game.setState(ROUND_END);
		}
	},
	TAKE_POWER {
		public void nextState(IGameLogic game) {
			game.getCurrTurn().independ();
			game.setState(ROUND_END);
		}
	},
	ROUND_END {
		public boolean isWaitInput() {
			return false;
		}

		public void nextState(IGameLogic game) {
			boolean isNewRound = game.nextTurn();
			if (!isNewRound) {
				GameMsg msg = new GameMsg(0);
				msg.addPlayer(game.getCurrTurn().getPlayerName());
				msg.add(" 回合开始.");
				game.getGameRecorder().add(msg);
			}

			game.setState((isNewRound) ? NEW_ROUND : SELECT_ACTION);
		}
	},
	WAITING {
		public boolean isWaitInput() {
			return false;
		}

		public void nextState(IGameLogic game) {
			return;
		}
	},
	DEFEATED {
		public void nextState(IGameLogic game) {
			game.setState(GAME_START);
		}
	},
	VICTORY {
		public void nextState(IGameLogic game) {
			game.setState(GAME_START);
		}
	};

	public static AnimManager getAnimManager(IGameLogic game) {
		return ((GameLogic) game).getAnimManager();
	}

	public static void addAnimation(IGameLogic game, int animId) {
		AnimManager animManager = getAnimManager(game);
		animManager.addAnimation(animId);
	}

	public static void addAnimation(IGameLogic game, IPlayable anim) {
		AnimManager animManager = getAnimManager(game);
		animManager.addAnimation(anim);
	}

	public boolean isWaitInput() {
		return true;
	}

	public static final String TAG = "GameState";
}
